package com.yang.chen.oa.serviceImpl;

import com.alibaba.fastjson.JSON;
import com.yang.chen.oa.domain.*;
import com.yang.chen.oa.mapper.TbSignMapper;
import com.yang.chen.oa.service.SignService;
import com.yang.chen.oa.vo.PageBean;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

@Service
public class SignServiceImpl implements SignService {
    @Autowired
    private TbSignMapper mapper;

    @Override
    public PageBean<TbSign> selectPage(Integer pageNumber, Integer pageSize, Date startDate, Date endDate, TbSign sign) {
            System.out.println(JSON.toJSONString(sign));
        TbSignExample example = new TbSignExample();
        //增加查询条件
        TbSignExample.Criteria criteria = example.createCriteria();
        if(null!=sign&& null!=sign.getCreatetime()){
            criteria.andCreatetimeEqualTo(sign.getCreatetime());
        }
        if(null!=startDate && null==endDate){
            criteria.andCreatetimeBetween(startDate,endDate);
        }
        if(null!=sign&& null!=sign.getUid()){
            criteria.andUidEqualTo(sign.getUid());
        }
        if (null==pageNumber)
            pageNumber=1;
        if (null==pageSize)
            pageNumber=10;

        example.setStart((pageNumber-1)*pageSize);
        example.setLimit(pageSize);
        //增加排序
        example.setOrderByClause("createTime desc");//通过产品ID，降序排列

        List<TbSign> tbSignList=mapper.selectPageByExample(example);
        long count=mapper.countByExample(example);
        PageBean<TbSign> pageBean = new PageBean<TbSign>();

        if(CollectionUtils.isEmpty(tbSignList)){
            System.out.println("查询结果为空");
            pageBean.setTotal(count);
            pageBean.setRows(new ArrayList<>());
        }else {
            pageBean.setTotal(count);
            pageBean.setRows(tbSignList);
        }
        return pageBean;
    }

    @Override
    public boolean addTbSign(TbSign job) {
        return mapper.insertSelective(job) > 0;
    }

    @Override
    public boolean selectTbSign(TbSign sign) {
        TbSignExample example = new TbSignExample();
        //增加查询条件
        TbSignExample.Criteria criteria = example.createCriteria();
        if(null!=sign&& null!=sign.getCreatetime()){
            criteria.andCreatetimeEqualTo(sign.getCreatetime());
        }
        if(null!=sign&& null!=sign.getUid()){
            criteria.andUidEqualTo(sign.getUid());
        }
        return mapper.countByExample(example) > 0;
    }

    @Override
    public boolean deleteById(Integer id) {
        return mapper.deleteByPrimaryKey(id) > 0;
    }

    @Override
    public boolean deleteBybatch(Integer[] ids) {
        TbSignExample example = new TbSignExample();
        example.createCriteria().andIdIn(Arrays.asList(ids));
        return mapper.deleteByExample(example) > 0;
    }

}
